package ru.compft.web.utils;

import java.util.Enumeration;
import java.util.ResourceBundle;

/**
 * User: VVasiliev
 * Date: 07.06.12
 * Time: 16:09
 * Хранитель версии приложения
 */
public class VersionHolder extends ResourceBundle {

    protected static final String BUNDLE_NAME = "ru.compft.resources.version";
//    protected static final String BUNDLE_EXTENSION = "properties";
//    protected static final ResourceBundle.Control VERSION_CONTROL = new VersionControl();

    public VersionHolder() {
        setParent(ResourceBundle.getBundle(BUNDLE_NAME));
    }

    @Override
    protected Object handleGetObject(String key) {
        return parent.getObject(key);
    }

    @Override
    public Enumeration<String> getKeys() {
        return parent.getKeys();
    }

//    protected static class VersionControl extends Control {
//        public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IllegalAccessException, InstantiationException, IOException {
//            // The below code is copied from default Control#newBundle() implementation.
//            // Only the PropertyResourceBundle line is changed to read the file as UTF-8.
//            String bundleName = toBundleName(baseName, locale);
//            String resourceName = toResourceName(bundleName, BUNDLE_EXTENSION);
//            ResourceBundle bundle = null;
//            InputStream stream = null;
//            if (reload) {
//                URL url = loader.getResource(resourceName);
//                if (url != null) {
//                    URLConnection connection = url.openConnection();
//                    if (connection != null) {
//                        connection.setUseCaches(false);
//                        stream = connection.getInputStream();
//                    }
//                }
//            } else {
//                stream = loader.getResourceAsStream(resourceName);
//            }
//            if (stream != null) {
//                try {
//                    bundle = new PropertyResourceBundle(new InputStreamReader(stream, GlobalConstants.DEFAULT_ENCODING));
//                } finally {
//                    stream.close();
//                }
//            }
//            return bundle;
//        }
//    }
}
